package com.glela.order.service.impl.vo;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.MapUtils;

import com.glela.common.constant.GoodsSelfTypeEnum;
import com.glela.platform.vo.GoodsSimpleVo;

public class OrderVo {
	private long userId; //用户id

	private boolean	hasSelfGoods;	//有自主产品
	private boolean	hasNoSelfGoods;	//有非自主产品

	private long	totalPricePromotion;//可算优惠的总商品金额
	private long	fullCutFee;			//满减金额
	private long	redPacketFee;		//红包费用

	private Set<Object>						brandIdSet			= new LinkedHashSet<>();	//品牌id集合
	private Set<Object>						goodsIdSet			= new LinkedHashSet<>();	//商品id集合
	private Map<String, Map<Long, Long>>	_total_amount_map	= new LinkedHashMap<>();	//各种分类统计销售总金额

	/** 仓库分类前缀 */
	private static final String	PREF_STORE_ID				= "store_id_";
	/** 各仓库总计销售金额 */
	private static final String	KEY_STORE_ID_TOTAL_AMOUNT	= PREF_STORE_ID;

	/** 商品goodsid分类前缀 */
	private static final String	PREF_GOODS_ID				= "goods_id";
	/** 各仓库商品goodsid总计销售金额 */
	private static final String	KEY_GOODS_ID_TOTAL_AMOUNT	= PREF_GOODS_ID;

	/** 品牌分类前缀 */
	private static final String	PREF_BRAND_ID				= "brand_id";
	/** 各仓库品牌总计销售金额 */
	private static final String	KEY_BRAND_ID_TOTAL_AMOUNT	= PREF_BRAND_ID;

	/*各仓库销售总金额*/
	private Map<Long, Long> getStoreIdTotalAmountMap() {
		return get_total_amount_map().get(KEY_STORE_ID_TOTAL_AMOUNT);
	}

	/*各品牌销售总金额*/
	private Map<Long, Long> getBrandIdTotalAmountMap() {
		return get_total_amount_map().get(KEY_BRAND_ID_TOTAL_AMOUNT);
	}

	/*各商品goodsID销售总金额*/
	private Map<Long, Long> getGoodsIdMap() {
		return get_total_amount_map().get(KEY_GOODS_ID_TOTAL_AMOUNT);
	}

	public OrderVo(long userId) {
		super();
		setUserId(userId);
		initTotalAmountMap();
	}

	private void initTotalAmountMap() {
		get_total_amount_map().put(KEY_STORE_ID_TOTAL_AMOUNT, new LinkedHashMap<Long, Long>());
		get_total_amount_map().put(KEY_GOODS_ID_TOTAL_AMOUNT, new LinkedHashMap<Long, Long>());
		get_total_amount_map().put(KEY_BRAND_ID_TOTAL_AMOUNT, new LinkedHashMap<Long, Long>());
	}

	/** 设置值 */
	public void setValue(int proTotalAmount, int storeId, int selfSupport, long goodsId, int brandId) {
		setValue(proTotalAmount, storeId, selfSupport, goodsId, brandId, false);
	}

	public void setValue(int proTotalAmount, int storeId, int selfSupport, long goodsId, int brandId, boolean isOpenGoods) {
		if (!isHasSelfGoods() && selfSupport == GoodsSelfTypeEnum.T0.getCode()) {
			setHasSelfGoods(true);
		}
		if (!isHasNoSelfGoods() && selfSupport == GoodsSelfTypeEnum.T1.getCode()) {
			setHasNoSelfGoods(true);
		}
		acumStoreTotalAmount(storeId, proTotalAmount);

		if (isOpenGoods) {} else {
			setTotalPricePromotion(getTotalPricePromotion() + proTotalAmount);
			acumTotalAmountGoodsIdMap(goodsId, proTotalAmount);
			acumTotalAmountGrandIdMap(brandId, proTotalAmount);
			brandIdSet.add(brandId);
			goodsIdSet.add(goodsId);
		}
	}

	public void setValue(GoodsSimpleVo valGoods) {
		setValue(valGoods.getSalePrice() * valGoods.getQuantity(), valGoods.getStoreId(), valGoods.getSelfSupport(), valGoods.getGoodId(), valGoods.getBrandId(), valGoods.isOpenGoods());
	}

	public void clear() {
		setTotalPricePromotion(0);
		setHasNoSelfGoods(false);
		setHasNoSelfGoods(false);

		getBrandIdSet().clear();
		getGoodsIdSet().clear();

		get_total_amount_map().clear();
		initTotalAmountMap();
	}

	/** 销售总价 */
	public int getTotalPrice() {
		int totalPrice = 0;
		if (MapUtils.isNotEmpty(getStoreIdTotalAmountMap())) {
			Collection<Long> values = getStoreIdTotalAmountMap().values();
			for (Long long1 : values) {
				totalPrice += long1;
			}
		}
		return totalPrice;
	}

	/** 根据店铺id获取该店铺总销售金额 */
	public long getStoreTotalAmount(int storeId) {
		return MapUtils.getLongValue(getStoreIdTotalAmountMap(), storeId + 0l, 0);
	}

	/** 根据商品id获取该商品总销售金额 */
	public long getGoodsTotalAmount(long goodsId) {
		return MapUtils.getLongValue(getGoodsIdMap(), goodsId, 0);
	}

	/** 根据品牌id获取该品牌总销售金额 */
	public long getBrandTotalAmount(long brandId) {
		return MapUtils.getLongValue(getBrandIdTotalAmountMap(), brandId, 0);
	}

	private void acumStoreTotalAmount(long storeId, long amount) {
		getStoreIdTotalAmountMap().put(storeId, MapUtils.getLongValue(getStoreIdTotalAmountMap(), storeId, 0) + amount);
	}

	private void acumTotalAmountGoodsIdMap(long goodsId, long amount) {
		getGoodsIdMap().put(goodsId, MapUtils.getLongValue(getGoodsIdMap(), goodsId, 0) + amount);
	}

	private void acumTotalAmountGrandIdMap(long brandId, long amount) {
		getBrandIdTotalAmountMap().put(brandId, MapUtils.getLongValue(getBrandIdTotalAmountMap(), brandId, 0) + amount);
	}

	//**********************************************************//

	public long getUserId() {
		return userId;
	}

	private Map<String, Map<Long, Long>> get_total_amount_map() {
		return _total_amount_map;
	}

	/** 有自主商品 */
	public boolean isHasSelfGoods() {
		return hasSelfGoods;
	}

	/** 有非自主商品 */
	public boolean isHasNoSelfGoods() {
		return hasNoSelfGoods;
	}

	/** 可优惠商品总金额 */
	public long getTotalPricePromotion() {
		return totalPricePromotion;
	}

	private void setTotalPricePromotion(long totalPricePromotion) {
		this.totalPricePromotion = totalPricePromotion;
	}

	public Set<Object> getBrandIdSet() {
		return brandIdSet;
	}

	public Set<Object> getGoodsIdSet() {
		return goodsIdSet;
	}

	private void setUserId(long userId) {
		this.userId = userId;
	}

	private void setHasSelfGoods(boolean hasSelfGoods) {
		this.hasSelfGoods = hasSelfGoods;
	}

	private void setHasNoSelfGoods(boolean hasNoSelfGoods) {
		this.hasNoSelfGoods = hasNoSelfGoods;
	}

	/** 满减金额 */
	public long getFullCutFee() {
		return fullCutFee;
	}

	/** 满减金额 */

	public void setFullCutFee(long fullCutFee) {
		this.fullCutFee = fullCutFee;
	}

	/** 红包费用 */
	public long getRedPacketFee() {
		return redPacketFee;
	}

	/** 红包费用 */

	public void setRedPacketFee(long redPacketFee) {
		this.redPacketFee = redPacketFee;
	}

}
